﻿using Lgc_Shop.Models;
using Lgc_Shop.Models.Database;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using static Microsoft.AspNetCore.Razor.Language.TagHelperMetadata;

namespace Lgc_Shop.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CommodityController : ControllerBase
    {
        // 定义数据库上下文
        private readonly Lgc_ShopContext _db;

        /// <summary>
        /// 构造方法 构造注入
        /// 把数据库上下文初始化
        /// </summary>
        /// <param name="db"></param>
        public CommodityController(Lgc_ShopContext db)
        {
            _db = db;
        }

        /// <summary>
        /// 商品添加
        /// </summary>
        /// <param name="ComName">商品名称</param>
        /// <param name="ComState">商品状态(0下架 1上架 留空为0下架)</param>
        /// <param name="ComCateNo">商品分类</param>
        /// <param name="ComPhoto">商品图片</param>
        /// <param name="ComCount">商品数量</param>
        /// <param name="ComPrice">商品价格</param>
        /// <param name="ComRefer">商品描述</param>
        /// <returns></returns>
        [HttpGet]
        public string AddCommodity(string ComName, int ComState, int ComCateNo, string ComPhoto, int ComCount, decimal ComPrice, string ComRefer)
        {
            if (ComName == null || ComCateNo == 0 || ComPhoto == null || ComCount == 0 || ComPrice == 0 || ComRefer == null)
            {
                return "请输入完整信息";
            }
            if (_db.CommodityCategorizes.Find(ComCateNo) == null)
            {
                return "分类不存在";
            }
            var commodity = new Commodity
            {
                ComName = ComName,
                ComState = ComState,
                ComCateNo = ComCateNo,
                ComPhoto = ComPhoto,
                ComCount = ComCount,
                ComPrice = ComPrice,
                ComRefer = ComRefer,
                ComCreateTime = System.DateTime.Now
            };
            _db.Commodities.Add(commodity);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "添加成功";
            }
            else
            {
                return "添加失败";
            }
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="ComId">商品编号</param>
        /// <returns></returns>
        [HttpGet]
        public string DeleteCommodity(int ComId)
        {
            var commodity = _db.Commodities.Find(ComId);
            if (commodity == null)
            {
                return "商品不存在";
            }
            _db.Commodities.Remove(commodity);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "删除成功";
            }
            else
            {
                return "删除失败";
            }
        }

        /// <summary>
        /// 商品查询(编号、标题关键字二选一或都填)
        /// </summary>
        /// <param name="Comid">商品编号</param>
        /// <param name="key">商品关键字</param>
        /// <returns></returns>
        [HttpGet]
        public List<Commodity> GetCommodity(int Comid, string key)
        {
            var commodities = _db.Commodities.ToList();
            if (commodities.Count == 0)
            {
                return null;
            }
            if (Comid != 0)
            {
                commodities = commodities.Where(x => x.ComNo == Comid).ToList();
            }
            if (key != null)
            {
                //区分key内容的大小写进行查询
                //commodities = commodities.Where(x => x.ComName.Contains(key)).ToList();
                //不区分key内容的大小写进行查询
                commodities = commodities.Where(x => x.ComName.ToLower().Contains(key.ToLower())).ToList();
            }

            return commodities;
        }

        /// <summary>
        /// 修改商品
        /// </summary>
        /// <param name="ComId">商品编号</param>
        /// <param name="ComName">商品名称</param>
        /// <param name="ComState">商品状态(0下架 1上架 留空为0下架)</param>
        /// <param name="ComPhoto">商品图片</param>
        /// <param name="ComCount">商品数量(库存)</param>
        /// <param name="ComPrice">商品价格</param>
        /// <param name="ComRefer">商品描述</param>
        /// <returns></returns>
        [HttpGet]
        public string UpdateCommodity(int ComId, string ComName, int ComState, string ComPhoto, int ComCount, decimal ComPrice, string ComRefer)
        {
            var commodity = _db.Commodities.Find(ComId);
            if (commodity == null)
            {
                return "商品不存在";
            }
            var comstate = _db.Commodities.FirstOrDefault(x => x.ComNo == ComId).ComState;
            
            if (ComName == null && ComPhoto == null && ComCount == 0 && ComPrice == 0 && ComRefer == null && comstate == ComState)
            {
                if (ComState == 1)
                {
                    return "商品已上架，无法修改";
                }
                if (ComState == 0)
                {
                    return "商品已下架，无法修改";
                }
            }
            if (ComName != null)
            {
                commodity.ComName = ComName;
            }
            if (ComPhoto != null)
            {
                commodity.ComPhoto = ComPhoto;
            }
            if (ComCount != 0)
            {
                commodity.ComCount = ComCount;
            }
            if (ComPrice != 0)
            {
                commodity.ComPrice = ComPrice;
            }
            if (ComRefer != null)
            {
                commodity.ComRefer = ComRefer;
            }
            commodity.ComState = ComState;
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "修改成功";
            }
            else
            {
                return "修改失败";
            }
        }

        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="ComName">分类名称</param>
        /// <returns></returns>
        [HttpGet]
        public string AddCommodityCategorize(string ComName)
        {
            var comname = _db.CommodityCategorizes.Count(x => x.ComCateName == ComName);
            if (comname > 0)
            {
                return "该分类已存在";
            }
            CommodityCategorize commodityCategorize = new CommodityCategorize
            {
                ComCateName = ComName
            };
            _db.CommodityCategorizes.Add(commodityCategorize);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "添加成功";
            }
            else
            {
                return "添加失败";
            }
        }

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="ComCateId">分类编号</param>
        /// <param name="ComCateName">分类名称</param>
        /// <returns></returns>
        [HttpGet]
        public string DeleteCommodityCategorize(int ComCateId, string ComCateName)
        {
            if (ComCateId == 0 && ComCateName == null)
            {
                return "请输入删除项";
            }
            if (ComCateId != 0 && ComCateName != null) // 两个框都有值
            {
                var d = _db.CommodityCategorizes.Count(x => x.ComCateNo == ComCateId && x.ComCateName == ComCateName);
                if (d == 0)
                {
                    return "不存在此分类组合";
                }
                else
                {
                    _db.CommodityCategorizes.Remove(_db.CommodityCategorizes.FirstOrDefault(x => x.ComCateNo == ComCateId && x.ComCateName == ComCateName));
                    var result = _db.SaveChanges();
                    if (result > 0)
                    {
                        return "删除成功";
                    }
                    else
                    {
                        return "删除失败";
                    }
                }
            }
            else if (ComCateId > 0 && ComCateName == null) // 只有编号
            {
                var commodityCategorize = _db.CommodityCategorizes.Find(ComCateId);
                if (commodityCategorize == null)
                {
                    return "分类不存在";
                }
                _db.CommodityCategorizes.Remove(commodityCategorize);
                var row = _db.SaveChanges();
                if (row > 0)
                {
                    return "删除成功";
                }
                else
                {
                    return "删除失败";
                }
            }
            else // 只有名称
            {
                var re = _db.CommodityCategorizes.Count(x => x.ComCateName == ComCateName);
                if (re == 0)
                {
                    return "分类不存在";
                }
                else
                {
                    _db.CommodityCategorizes.Remove(_db.CommodityCategorizes.FirstOrDefault(x => x.ComCateName == ComCateName));
                    var row = _db.SaveChanges();
                    if (row > 0)
                    {
                        return "删除成功";
                    }
                    else
                    {
                        return "删除失败";
                    }
                }
            }

        }

        /// <summary>
        /// 分类查询(无则返回null)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<CommodityCategorize> GetCommodityCategorize(string key)
        {
            var commodityCategorizes = _db.CommodityCategorizes.ToList();
            if (commodityCategorizes.Count == 0)
            {
                return null;
            }
            if (key != null)
            {
                //不区分key内容的大小写进行查询
                commodityCategorizes = commodityCategorizes.Where(x => x.ComCateName.ToLower().Contains(key.ToLower())).ToList();
            }
            return commodityCategorizes;
        }

        /// <summary>
        /// 修改分类
        /// </summary>
        /// <param name="ComCateId">分类编号</param>
        /// <param name="ComCateName">分类名称</param>
        /// <returns></returns>
        [HttpGet]
        public string UpdateCommodityCategorize(int ComCateId, string ComCateName)
        {
            var commodityCategorize = _db.CommodityCategorizes.Find(ComCateId);
            if (commodityCategorize == null)
            {
                return "分类不存在";
            }
            if (ComCateName == null)
            {
                return "请输入修改项";
            }
            commodityCategorize.ComCateName = ComCateName;
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "修改成功";
            }
            else
            {
                return "修改失败";
            }
        }

        /// <summary>
        /// 前端商品分类列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<CommodityCategorize> GetCommodityCategorizes()
        {
            if (_db.CommodityCategorizes.Count() == 0)
            {
                return null;
            }
            return _db.CommodityCategorizes.ToList();
        }

        /// <summary>
        /// 前端商品列表
        /// </summary>
        /// <param name="ComCateNo">商品分类编号</param>
        /// <param name="key">商品关键字</param>
        /// <param name="page">页码</param>
        /// <param name="size">每页数量</param>
        /// <returns></returns>
        [HttpGet]
        public List<Commodity_GetCommodityByComCateNoOrKey> GetCommodityByComCateNoOrKey(int ComCateNo, string key, int page, int size)
        {
            var commodities = _db.Commodities.ToList();
            if (commodities.Count == 0)
            {
                return null;
            }
            if (ComCateNo != 0)
            {
                commodities = commodities.Where(x => x.ComCateNo == ComCateNo).ToList();
            }
            if (key != null)
            {
                commodities = commodities.Where(x => x.ComName.ToLower().Contains(key.ToLower())).ToList();
            }
            // 计算最大页数
            int maxPage = 1;
            if (commodities.Count % size == 0)
            {
                maxPage = commodities.Count / size;
            }
            else
            {
                maxPage = commodities.Count / size + 1;
            }

            if (page <= 0 || size <= 0 || page > maxPage)
            {
                return null;
            }
            var result = commodities.Skip((page - 1) * size).Take(size).ToList();
            List<Commodity_GetCommodityByComCateNoOrKey> resultList = new List<Commodity_GetCommodityByComCateNoOrKey>();
            foreach (var item in result)
            {
                resultList.Add(new Commodity_GetCommodityByComCateNoOrKey
                {
                    ComNo = item.ComNo,
                    ComName = item.ComName,
                    ComPhoto = item.ComPhoto,
                    ComCount = item.ComCount,
                    NowPage = page.ToString(),
                    PageCount = maxPage.ToString()
                });
            }
            return resultList;
        }

        /// <summary>
        /// 前端商品详情
        /// </summary>
        /// <param name="ComNo">商品编号</param>
        /// <returns></returns>
        [HttpGet]
        public List<Commodity_GetCommodityByComNo> GetCommodityByComNo(int ComNo)
        {
            var commodities = _db.Commodities.ToList();
            if (commodities.Count == 0)
            {
                return null;
            }
            if (ComNo != 0)
            {
                commodities = commodities.Where(x => x.ComNo == ComNo).ToList();
            }
            List<Commodity_GetCommodityByComNo> resultList = new List<Commodity_GetCommodityByComNo>();
            foreach (var item in commodities)
            {
                resultList.Add(new Commodity_GetCommodityByComNo
                {
                    ComName = item.ComName,
                    ComPhoto = item.ComPhoto,
                    ComCount = item.ComCount,
                    ComPrice = item.ComPrice,
                    ComRefer = item.ComRefer
                });
            }
            return resultList;
        }
    }
}
